<template>
  <div class="update-name">
    <!-- 导航栏 -->
    <van-nav-bar title="设置昵称" left-text="取消" right-text="完成" @click-left="$emit('close')" @click-right="onConfirm"></van-nav-bar>
    <!-- 设置 maxlength 和 show-word-limit 属性后会在底部显示字数统计 -->
    <div class="field-wrap">
      <van-field v-model.trim="localname" rows="2" autosize type="textarea" maxlength="10" placeholder="请输入昵称" show-word-limit />
    </div>
  </div>

</template>

<script>
import { updateUserProfile } from '@/api/user.js'
export default {
  name: 'UpdateName',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      localname: this.value
    }
  },

  mounted() {},

  methods: {
    async onConfirm() {
      this.$toast.loading = {
        message: '保存中',
        forbidclick: true, //禁止背景点击
        duration: 0 //持续时间
      }

      try {
        const localname = this.localname
        if (!localname.length) {
          return this.$toast('禁止为空')
        }
        await updateUserProfile({
          name: localname
        })
        //  console.log(data);
        // 更新视图，
        this.$emit('input', localname)
        // 关闭弹层
        this.$emit('close')
        // 提示成功
        this.$toast.success('更新成功')
      } catch (error) {
        this.$toast('更新失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
.field-wrap {
  padding: 10px;
}
</style>